package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
//        获取用户输入的id
        String password = employee.getPassword();
//        对获取的id进行加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
//        比较表里的用户名 和用户输入的用户名 是否相同
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
//        封装成实体类对象
        Employee one = employeeService.getOne(queryWrapper);
//        进行判断  如果数据为空 则返回登录失败
        if (one == null) {
            return R.error("登录失败");
        }

//       数据库数据的密码和获取用户输入的密码进行比较 不同则返回密码错误
        if (!one.getPassword().equals(password)) {
            return R.error("密码错误");
        }

//        判断当前用户的账号是否为禁用状态
        if (one.getStatus() == 0) {
            return R.error("账号已禁用");
        }
//        登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    /**
     * 退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
//        清理Session中之前员工的登录id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * 添加员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee) {
//        设置用户的初始密码并进行加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        调用添加的方法 使用自动装配
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
//        创建分页对象 记得配置分页bean
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
//        模糊查询 先判断name是否为空
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
//        设置排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 启用禁用 修改
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){

        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
            return R.success(employee);
    }


}
